package rai.blo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import rai.util.Constants;

public class RegFormBLO {
    private static Logger log = Logger.getLogger("InputChecking");
    
    public boolean checkUser(String name){
        Connection conn = null;
        String query = Constants.BLANK_VALUE;
        ResultSet rs = null;
        PreparedStatement pStat = null;
        boolean check = false;
        
        try{
            query = "select * from user where LOWER(Username)=?";
            conn = rai.util.Connection.getConnection();
            pStat = conn.prepareStatement(query);
            log.info(name.toLowerCase());
            pStat.setString(1, name.toLowerCase());
            rs = pStat.executeQuery();
            if (rs.next()) {
                check = true;
            }
        }
        catch (Exception sqlEx) {
            log.error("Register.getUser " + sqlEx);
        } 
        finally {
            try {
                rs.close();
                pStat.close();
                conn.close();
            } 
            catch (SQLException sqlEx) {
                log.error("Register.getUser " + sqlEx);
            }
        }
        return check;
    }
    
    /**
     * check whenever Password does not have the right type.
     *
     * @param Pass
     * @return error
     */
    public final String checkPass(final String pass) {
        String error = null;
        if (pass.length() < 4) {
            error = "Password must has at least 4 character";
        } else {
            for (int i = 0; i < pass.length(); i++) {
                if (pass.charAt(i) >= '0' && pass.charAt(i) <= '9') {
                    continue;
                } else if (pass.charAt(i) >= 'a' && pass.charAt(i) <= 'z') {
                    continue;
                } else if (pass.charAt(i) >= 'A' && pass.charAt(i) <= 'Z') {
                    continue;
                } else {
                    error = "Password only contains numbers and letters";
                    break;

                }
            }
        }
        return error;
    }
    
    /**
     * check whenever Username does not have the right type.
     *
     * @param Pass
     * @return error
     */
    public final String checkUsername(final String pass) {
        String error = null;
        if (pass.length() < 4) {
            error = "Username must has at least 4 character";
        } else {
            for (int i = 0; i < pass.length(); i++) {
                if (pass.charAt(i) >= '0' && pass.charAt(i) <= '9') {
                    continue;
                } else if (pass.charAt(i) >= 'a' && pass.charAt(i) <= 'z') {
                    continue;
                } else if (pass.charAt(i) >= 'A' && pass.charAt(i) <= 'Z') {
                    continue;
                } else {
                    error = "Username only contains numbers and letters";
                    break;

                }
            }
        }
        return error;
    }
    
    
    
}